package cn.edu.sdju.wanglo1911.team10.utillity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import cn.edu.sdju.wanglo1911.team10.po.QueryOption;

public class Helper {
	public static String getBackUrl(HttpServletRequest request) {
		String contextPath = request.getContextPath();
		int contextPathLength = contextPath.length();
		int idx = request.getHeader("Referer").indexOf(contextPath);
		if (idx == -1) {
			return "";
		}
		String backURL = request.getHeader("Referer").substring(idx+contextPathLength);
		return backURL;
	}
	public static String encodeQueryOption(QueryOption option) {
		String query = option.getQuery() == null ? "" : option.getQuery();
		String queryType = option.getQueryType() == null ? "" : option.getQueryType();
		return String.format("order=%s&dir=%d&query=%s&queryType=%s", option.getOrder(), option.getDir(), query,queryType);
	}
	
	public static String genOrderNo() {
		Date dNow = new Date();
	    SimpleDateFormat ft = new SimpleDateFormat ("yyyyMMdd");
	    String uuid = UUID.randomUUID().toString().substring(0, 32);
		String orderNo = "NO" + ft.format(dNow) + uuid;
		return orderNo;
	}
	public static String getExt(String fileName) {
		int idx = fileName.lastIndexOf('.');
		return fileName.substring(idx, fileName.length());
	}
}
